package com.zaofan.service;

import com.zaofan.bean.Complaints;
import com.zaofan.bean.GoodsOrderInfo;
import com.zaofan.bean.OrderInfo;
import com.zaofan.dao.ComplaintsDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.WebUtils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ComplaintsService {
	private ComplaintsDao complaintsDao = new ComplaintsDao();
	private OrderInfoService orderInfoService = new OrderInfoService();
	private GoodsOrderInfoService goodsOrderInfoService = new GoodsOrderInfoService();

	public void delete(Complaints complaints) {
		complaintsDao.del(complaints);
	}

	public void add(Complaints complaints) {
		complaints.setAddTime(new Date());
		complaintsDao.add(complaints);
	}

	public void update(Complaints complaints) {
		complaintsDao.update(complaints);
	}

	public List<Complaints> findAll(Integer cityId, Map<String, Object> params,String selectCityId) {
		if (cityId == 0&&"0".equals(selectCityId)) {
			return findAll(params);
		}
		QueryRunner qr = new QueryRunner(C3p0DataSourceUtils.getDataSource());
		List<Complaints> data = null;
		try {
			data = qr.query("select complaints.* from complaints "
					+ " inner join orderinfo on orderinfo.orderNo = complaints.orderNo "
					+ " where orderinfo.cityId= " + selectCityId
					+ " and orderinfo.parentId = 0 "
					+ " order " +  params.get("order")
					+ " limit " + params.get("begin") + "," + params.get("end"),
					new BeanListHandler<Complaints>(Complaints.class) );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Integer cityId, Map<String, Object> params,String selectCityId) {
		if (cityId == 0 && "0".equals(selectCityId)) {
			return count(params);
		}
		QueryRunner qr = new QueryRunner(C3p0DataSourceUtils.getDataSource());
		try {
			return qr.query("select count(*) from complaints "
					+ " inner join orderinfo on orderinfo.orderNo = complaints.orderNo "
					+ " where orderinfo.cityId=" + selectCityId
					+ " and orderinfo.parentId = 0 "
					, new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public List<Complaints> findAll(Map<String, Object> params) {
		List<Complaints> data = complaintsDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return complaintsDao.count(params);
	}

	public Complaints findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return complaintsDao.findOne(params);
	}

	public void complaints(Integer orderId, String content) throws ServiceException {
		if (WebUtils.isEmpty(content)) {
			throw new ServiceException("请输入您的投诉内容!!");
		}
		OrderInfo order = orderInfoService.findById(orderId);
		if (order == null) {
			throw new ServiceException("订单信息有误!!");
		}
		Complaints old = findByOrderId(orderId);
		if (old != null) {
			throw new ServiceException("对不起,您已经对该订单投诉,我们也正在努力的为您解决!!!");
		}
		Complaints complaints = new Complaints();
		complaints.setAccountId(order.getAccountId());
		complaints.setAccountMobile(order.getAccountMobile());
		complaints.setAddTime(new Date());
		complaints.setContent(content);
		complaints.setOrderId(orderId);
		complaints.setOrderNo(order.getOrderNo());
		complaints.setOrderTime(order.getOrderTime());
		complaints.setState(0);
		add(complaints);
	}

	public void complaintsGoodOrder(Integer orderId, String content) throws ServiceException {
		if (WebUtils.isEmpty(content)) {
			throw new ServiceException("请输入您的投诉内容!!");
		}
		GoodsOrderInfo order = goodsOrderInfoService.findById(orderId);
		if (order == null) {
			throw new ServiceException("订单信息有误!!");
		}
		Complaints old = findByOrderId(orderId);
		if (old != null) {
			throw new ServiceException("对不起,您已经对该订单投诉,我们也正在努力的为您解决!!!");
		}
		Complaints complaints = new Complaints();
		complaints.setAccountId(order.getAccountId());
		complaints.setAccountMobile(order.getAccountMobile());
		complaints.setAddTime(new Date());
		complaints.setContent(content);
		complaints.setOrderId(orderId);
		complaints.setOrderNo(order.getOrderNo());
		complaints.setOrderTime(order.getOrderTime());
		complaints.setState(0);
		add(complaints);
	}

	private Complaints findByOrderId(Integer orderId) {
		Map<String, Object> params = new HashMap<>();
		params.put("orderId", orderId);
		return complaintsDao.findOne(params);
	}

}
